import {
  Column,
  Entity,
  PrimaryGeneratedColumn,
  ManyToMany,
  JoinTable,
} from 'typeorm';
import { ApiProperty } from '@nestjs/swagger';
import { CommonEntity } from 'src/common/entity/index.entity';
import { RoleEntity } from '../../role/entities/role.entity';

export enum CRUDEnum {
  CREATE = 'create',
  READ = 'read',
  UPDATE = 'update',
  DELETE = 'delete',
}

// // 默认访问等级，全局生效，角色使用其他字段覆盖控制
// export enum LevelEnum {
//   NOT = 'not', // 禁止访问
//   EVERYONE = 'everyone', // 允许任何人访问
//   ONLY_LOGIN = 'only_login', // 仅允许登录用户访问
//   ONLY_SPECIFIC_ONE = 'only_specific_one', // 仅允许特定用户访问
//   ONLY_SPECIFIC_DOMAIN = 'only_specific_domain', // 仅允许特定域名访问
// }

const CRUDArray = Object.values(CRUDEnum);

@Entity('permission', {})
export class PermissionEntity extends CommonEntity {
  @ApiProperty({ default: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' })
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @ApiProperty({ default: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' })
  @Column('uuid', { name: 'parent_id', nullable: true })
  parentId: string | null;

  @ApiProperty({ default: 'permission' })
  @Column('varchar', { name: 'name', length: 50, comment: '权限名' })
  title: string;

  @ApiProperty({ default: '/path' })
  @Column('varchar', { name: 'path', comment: '权限对应接口路径' })
  path: string;

  @ApiProperty({ default: CRUDArray })
  @Column('simple-enum', {
    name: 'crud',
    enum: CRUDEnum,
    comment: '接口增删改查允许类型',
  })
  crud: CRUDEnum[];

  // @ApiProperty({ default: LevelEnum.NOT })
  // @Column('enum', {
  //   name: 'status_level',
  //   default: LevelEnum.NOT,
  //   enum: LevelEnum,
  //   comment: '默认访问所需要的身份等级',
  // })
  // statusLevel: LevelEnum;

  @ApiProperty({ type: RoleEntity, isArray: true })
  @ManyToMany(() => RoleEntity)
  @JoinTable({
    name: 'role_permission_relation',
    joinColumn: {
      name: 'permission_id',
      referencedColumnName: 'id',
    },
    inverseJoinColumn: {
      name: 'role_id',
      referencedColumnName: 'id',
    },
  })
  roles: RoleEntity[];
}
